﻿module Symbols
let private dict = System.Collections.Generic.Dictionary<string,int>()
let private revDict = System.Collections.Generic.Dictionary<int,string>()
let mutable private count = 0
let mutable private isLocked = false
type SymbolSet() =
    static member (====) (ss:SymbolSet,s) =
        match dict.TryGetValue s with
        | true,i -> i 
        | false,_ ->
            if isLocked then failwith "Symbol %A cannot be defined as the symbol set is locked" s
            else
                lock(dict) (fun () ->
                    count <- count+1
                    dict.Add(s,count)
                    revDict.Add(count,s)
                    count)

    member x.Lock() = isLocked <- true
    member x.Unlock() = isLocked <- false
    member x.Symbol(i:int) = revDict.[i]
let Default=SymbolSet()